package com.smrs.goods.webapp.action;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;

import com.jof.framework.util.ExecuteResult;
import com.jof.framework.util.Pager;
import com.smrs.enums.StatusEnum;
import com.smrs.goods.model.GoodsAttributeModel;
import com.smrs.goods.model.GoodsAttributeValueModel;
import com.smrs.goods.model.GoodsCategoryModel;
import com.smrs.goods.model.GoodsItemModel;
import com.smrs.goods.model.GoodsSkuAttributeModel;
import com.smrs.goods.model.GoodsSkuModel;
import com.smrs.security.model.UserModel;
import com.smrs.util.AppUtil;



public class GoodsSkuAction extends BaseGoodsAction{
	private static final long serialVersionUID = 1L;
	protected GoodsSkuModel goodsSku = new GoodsSkuModel();
	protected static final String SESSION_KEY_GOODS_ITEM="SESSION_KEY_GOODS_ITEM";
	//protected 
	protected List<GoodsSkuModel> allGoodsSkuList = new ArrayList<GoodsSkuModel>();

	protected List<GoodsItemModel> allGoodsItemList = new ArrayList<GoodsItemModel>();
	protected List<GoodsAttributeModel> allGoodsAttributeList = new ArrayList<GoodsAttributeModel>();
	
	protected List<GoodsAttributeValueModel> allAttributeValueList = new ArrayList<GoodsAttributeValueModel>();
	protected String [] selectedAttributeValues;
	protected List<String> selectedValueList = new ArrayList<String>();
	protected String disableCheckbox="";
	protected Pager<GoodsSkuModel> pager = new Pager<GoodsSkuModel>(); 
	protected GoodsItemModel  goodsItem;
	protected List<GoodsCategoryModel> goodsCategoryLevel1List = new ArrayList<GoodsCategoryModel>();
	
	protected List<GoodsCategoryModel> allGoodsCategoryList = new ArrayList<GoodsCategoryModel>();
	private Long itemId;
	
	public List<GoodsCategoryModel> getGoodsCategoryLevel1List() {
		return goodsCategoryLevel1List;
	}

	public void setGoodsCategoryLevel1List(List<GoodsCategoryModel> goodsCategoryLevel1List) {
		this.goodsCategoryLevel1List = goodsCategoryLevel1List;
	}

	public List<GoodsCategoryModel> getAllGoodsCategoryList() {
		return allGoodsCategoryList;
	}

	public void setAllGoodsCategoryList(List<GoodsCategoryModel> allGoodsCategoryList) {
		this.allGoodsCategoryList = allGoodsCategoryList;
	}

	public GoodsItemModel getGoodsItem() {
		return goodsItem;
	}

	public void setGoodsItem(GoodsItemModel goodsItem) {
		this.goodsItem = goodsItem;
	}



	protected String titleName="产品信息";

	public String getDisableCheckbox() {
		return disableCheckbox;
	}

	public void setDisableCheckbox(String disableCheckbox) {
		this.disableCheckbox = disableCheckbox;
	}

	public List<String> getSelectedValueList() {
		return selectedValueList;
	}

	public void setSelectedValueList(List<String> selectedValueList) {
		this.selectedValueList = selectedValueList;
	}

	public String[] getSelectedAttributeValues() {
		return selectedAttributeValues;
	}

	public void setSelectedAttributeValues(String[] selectedAttributeValues) {
		this.selectedAttributeValues = selectedAttributeValues;
	}

	public List<GoodsAttributeModel> getAllGoodsAttributeList() {
		return allGoodsAttributeList;
	}

	public List<GoodsAttributeValueModel> getAllAttributeValueList() {
		return allAttributeValueList;
	}


	public void setAllGoodsAttributeList(
			List<GoodsAttributeModel> allGoodsAttributeList) {
		this.allGoodsAttributeList = allGoodsAttributeList;
	}

	public String[] getSelectedAttributes() {
		return selectedAttributeValues;
	}


	public void setSelectedAttributes(String[] selectedAttributes) {
		this.selectedAttributeValues = selectedAttributes;
	}


	public List<GoodsItemModel> getAllGoodsItemList() {
		return allGoodsItemList;
	}
	

	
	public Long getItemId() {
		return itemId;
	}


	public void setItemId(Long itemId) {
		this.itemId = itemId;
	}

	

	

	
	public void buildSelectedValueListByItemId(Long itemId){
		List<GoodsSkuAttributeModel> attrList = goodsSkuAttributeService.getSkuAttributeByItemId(itemId);
		if(CollectionUtils.isNotEmpty(attrList)){
			for(GoodsSkuAttributeModel attr:attrList){
				selectedValueList.add(attr.getAttributeValueId()+"");
			}
		}
	}
	
	public GoodsSkuModel buildSkuFromItem(GoodsItemModel goodsItem){
		GoodsSkuModel goodsSku = new GoodsSkuModel();
		goodsSku.setItemId(itemId);
		goodsSku.setItemCode(goodsItem.getCode());
		goodsSku.setBatchFlag(goodsItem.getBatchFlag());
		goodsSku.setMfgSkuCode(goodsItem.getMfgSkuCode());
		goodsSku.setSalesUnit(goodsItem.getSalesUnit());
		goodsSku.setStorageType(goodsItem.getStorageType());
		goodsSku.setValidFlag(goodsItem.getValidFlag());
		goodsSku.setSupplierId(goodsItem.getSupplierId());
		goodsSku.setPhysicalType(goodsItem.getPhysicalType());
		goodsSku.setStatus(StatusEnum.ACTIVE.getStatus());
		return goodsSku;
	}
	
	public String addSku(){
		goodsItem = goodsItemService.getByPK(itemId);
		goodsSku = buildSkuFromItem(goodsItem);
		supplier = supplierService.getByPK(goodsItem.getSupplierId());
		HashSet<GoodsAttributeModel> attributeSet = goodsCategoryService.getAllAttributeWithValueFromItem(goodsItem);
		allGoodsAttributeList.addAll(attributeSet);
		
		if(StringUtils.isEmpty(this.actionCommand)){			
			this.getSession().setAttribute(SESSION_KEY_GOODS_ITEM, goodsItem);
			buildSelectedValueListByItemId(itemId);
			return SUCCESS;
		}
		UserModel userModel = this.getUserModelFromSession();
		AppUtil.setCreateUserInfo(userModel, goodsSku);

		selectedValueList = new ArrayList<String>();
		for(int index=0;index<selectedAttributeValues.length;index++){
				selectedValueList.add(selectedAttributeValues[index]);
		}
		ExecuteResult<String> result = goodsSkuService.addSkuToDatabase(goodsSku, selectedAttributeValues); 
		if(!result.isSuccess()){
			this.addActionErrorsFromResult(result);			
		}else{
			this.addActionMessage("创建成功");
		}
		return SUCCESS;
	}
	

	
	public void buildAttributeToSku(List<GoodsSkuModel> list){
		if(CollectionUtils.isNotEmpty(list)){
			for(GoodsSkuModel oneModel : list){
				List<GoodsSkuAttributeModel> skuAttributeList=goodsSkuAttributeService.getSkuAttributeBySkuId(oneModel.getId());
				LinkedHashSet<GoodsSkuAttributeModel> set= new LinkedHashSet<GoodsSkuAttributeModel>();
				set.addAll(skuAttributeList);
				oneModel.setSkuAttributes(set);
			}
		}
	}
	
	public String searchSku(){
		//allGoodsSkuList=goodsSkuService.getAllActive();
		pager=goodsSkuService.getByNameLikePager(goodsSku.getName(),pager);
		List<GoodsSkuModel> list = pager.getRecords();
		buildAttributeToSku(list);
		return SUCCESS;
	}

	
	public String updateSku(){
		if(StringUtils.isEmpty(this.actionCommand)){
			allGoodsItemList = this.goodsItemService.getAllActive();
			goodsSku = goodsSkuService.getByPK(goodsSku.getId());			
		}else{
			UserModel userModel = this.getUserModelFromSession();
			AppUtil.setUpdateUserInfo(userModel, goodsSku);
			goodsSkuService.updateModel(goodsSku);
			this.addActionMessage("修改成功");			
			allGoodsItemList = this.goodsItemService.getAllActive();
		}
		
		GoodsItemModel model = goodsItemService.getByPK(goodsSku.getItemId());
		allGoodsAttributeList.addAll(goodsCategoryService.getAllAttributeWithValueFromItem(model));
		
		allGoodsItemList = new ArrayList<GoodsItemModel>();
		allGoodsItemList.add(model);
		List<GoodsSkuAttributeModel> skuAttributeList=goodsSkuAttributeService.getSkuAttributeBySkuId(goodsSku.getId());
		LinkedHashSet<GoodsSkuAttributeModel> set= new LinkedHashSet<GoodsSkuAttributeModel>();
		set.addAll(skuAttributeList);
		goodsSku.setSkuAttributes(set);
		if(CollectionUtils.isNotEmpty(skuAttributeList)){
			for(GoodsSkuAttributeModel oneModel:skuAttributeList){
				selectedValueList.add(oneModel.getAttributeValueId()+"");
			}
		}
		this.actionCommand="update";
		disableCheckbox="true";
		return SUCCESS;
	}
	
	public String deleteSku(){
		goodsSkuService.deleteModel(goodsSku);
		return "search";
	}
	
	public String viewSkuByItem(){
		Long itemId = this.goodsSku.getItemId();
		goodsItem = this.goodsItemService.getByPK(itemId);
		supplier = supplierService.getByPK(goodsItem.getSupplierId());
		pager.setPageSize(10000l);
		pager = this.goodsSkuService.findByItemId(itemId, pager);
		List<GoodsSkuModel> list = pager.getRecords();
		this.buildAttributeToSku(list);
		return SUCCESS;
	}
	public Pager<GoodsSkuModel> getPager() {
		return pager;
	}


	public void setPager(Pager<GoodsSkuModel> pager) {
		this.pager = pager;
	}

	public String getTitleName() {
		return titleName;
	}


	public void setTitleName(String titleName) {
		this.titleName = titleName;
	}

	public List<GoodsSkuModel> getAllGoodsSkuList() {
		return allGoodsSkuList;
	}


	public void setAllGoodsSkuList(
			List<GoodsSkuModel> allGoodsSkuList) {
		this.allGoodsSkuList = allGoodsSkuList;
	}


	public GoodsSkuModel getGoodsSku() {
		return goodsSku;
	}


	public void setGoodsSku(GoodsSkuModel goodsSku) {
		this.goodsSku = goodsSku;
	}
}
